package tests;

import common.AutoTestUtils;
import org.apache.hc.core5.util.Asserts;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.IOException;
import java.time.Duration;


public class LoginPage extends AutoTestUtils {
    public static String url = "http://120.26.160.116:8080/blog_login.html";


    public LoginPage() {
        super(url);
    }

    /**
     * 页面能正常加载
     */
    public void loginRight(){
        String text = driver.findElement(By.cssSelector("body > div.container-login > div > h3")).getText();
        assert "登陆".equals(text);
        driver.findElement(By.cssSelector("#submit"));
    }

    /**
     * 登录成功, 可以跳转到博客列表页
     */
    public void loginSuc() throws IOException {
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#password")).clear();
        driver.findElement(By.cssSelector("#username")).sendKeys("jjx");
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");
        driver.findElement(By.cssSelector("#submit")).click();

//        //先等待找到元素, 加载完成, 然后再进行判断
//        driver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)"));
//        String title = driver.getTitle();
// //       getScreenShot(getClass().getName());
//        assert title.equals("博客列表页");

        webDriverWait.until(ExpectedConditions.titleIs("博客列表页"));
        //回到登录页, 继续测试登录失败
        driver.navigate().back();

    }

    /**
     * 账号或密码为空
     * @throws IOException
     * @throws InterruptedException
     */
    public void loginNoUserNameOrPassword() throws IOException, InterruptedException {
        //成功后回退, 输入框会有内容, 先清空
        driver.navigate().refresh();
        driver.findElement(By.cssSelector("#username")).sendKeys("");
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");
        driver.findElement(By.cssSelector("#submit")).click();
        //getScreenShot(getClass().getName());
//        Thread.sleep(3000);

        //alertIsPresent 检查弹窗是否存在, 如果存在继续执行, 如果不存在, 报错
        //每次跳转弹窗时都加上最好...   不然总会报错
        webDriverWait.until(ExpectedConditions.alertIsPresent());

        Alert alert = driver.switchTo().alert();
        alert.accept();

        driver.navigate().refresh();
        driver.findElement(By.cssSelector("#username")).sendKeys("jjx");
        driver.findElement(By.cssSelector("#password")).sendKeys("");
        driver.findElement(By.cssSelector("#submit")).click();
//        Thread.sleep(3000);
        webDriverWait.until(ExpectedConditions.alertIsPresent());

        alert = driver.switchTo().alert();
        assert alert.getText().equals("账号或密码为空");
        alert.accept();

    }

    /**
     * 错误的账号或密码
     */
    public void loginErrUserOrPassword(){
        driver.navigate().refresh();
        driver.findElement(By.cssSelector("#username")).sendKeys("jjj");
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");
        driver.findElement(By.cssSelector("#submit")).click();

        webDriverWait.until(ExpectedConditions.alertIsPresent());
        Alert alert = driver.switchTo().alert();
        assert alert.getText().equals("用户不存在");
        alert.accept();

        driver.navigate().refresh();
        driver.findElement(By.cssSelector("#username")).sendKeys("jjx");
        driver.findElement(By.cssSelector("#password")).sendKeys("123");
        driver.findElement(By.cssSelector("#submit")).click();
        webDriverWait.until(ExpectedConditions.alertIsPresent());

        alert = driver.switchTo().alert();
        assert alert.getText().equals("密码错误");
        alert.accept();

    }

    public void registryButtonSuc(){
        driver.findElement(By.cssSelector("body > div.container-login > div > div:nth-child(5) > a")).click();

        //urlToBe   判断url是否正确
        webDriverWait.until(ExpectedConditions.urlToBe("http://120.26.160.116:8080/blog_registry.html"));
        //使用下面的方法会报错
//        String currentUrl = driver.getCurrentUrl();
//        assert currentUrl.equals("http://120.26.160.116:8080/blog_registry.html");
    }
}
